package com.Object类.toString方法;

public class toString_ {
    public static void main(String[] args) {
        /**Object类中的toString（）方法原码：
         * 默认返回：全类名+@+16进制哈希值     全类名指包名+类名（例如：本类中A类的全类名 = com.Object类.toString方法.A ）
         * public String toString() {
         *         return getClass().getName() + "@" + Integer.toHexString(hashCode());
         *     }
         */
        A a = new A("小明", "程序员", 10000);
        System.out.println(a.toString());//未重写toString方法，会调用Object的同String方法； 输出：全类名+@+16进制哈希值
        System.out.println(a);  //直接输出对象时，默认调用toString方法；跟a.toString()效果一样
        System.out.println("==================================");
        B b = new B("小红", 18, 10000);
        System.out.println(b.toString());  //重写了toString方法，会调用B的toString方法； 输出：我们自定义的toString方法；
        System.out.println(b);//直接输出对象时，默认调用toString方法；跟b.toString()效果一样

    }
}
 class A{
    private String name;
    private String job;
    private double salary;

     public A(String name, String job, double salary) {
         this.name = name;
         this.job = job;
         this.salary = salary;
     }
 }


 class B{
     private String name;
     private int age;
     private double salary;

     public B(String name, int age, double salary) {
         this.name = name;
         this.age = age;
         this.salary = salary;
     }

     @Override
     public String toString() {  //重写toString方法，输出对象的属性
         return "B{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 ", salary=" + salary +
                 '}';
     }


 }